<?php

namespace Vavilen\FirstBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Vavilen\FirstBundle\Entity\Post;
use Vavilen\FirstBundle\Form\PostForm;
use Vavilen\FirstBundle\Entity\Category;
use Vavilen\FirstBundle\Form\CategoryForm;
use Symfony\Component\HttpFoundation\Request;

use Doctrine\Common\Util\Debug; // DEBUG

class AdminController extends Controller
{
	public function indexAction()
	{
		$em = $this->get('doctrine')->getEntityManager();
		$posts = $em->getRepository('VavilenFirstBundle:Post')->findAll();

		return $this->render('VavilenFirstBundle:Admin:index.html.twig', array(
		    'posts' => $posts
		));
	}

	public function createPostAction(Request $request)
	{
		$form = $this->createForm(new PostForm(), new Post());
		if ($request->getMethod() == 'POST') {
			$form->bind($request);
			if ($form->isValid()) {
				$post = $form->getData();
				$post->setUser($this->getUser());
				$em = $this->getDoctrine()->getManager();
				$em->persist($post);
				$em->flush();
				return $this->redirect($this->generateUrl('admin'));
			}
		}

		return $this->render('VavilenFirstBundle:Admin:createpost.html.twig', array(
		    'form' => $form->createView(),
		));
	}

	public function editPostAction(Request $request, $postId)
	{
		$post = $this->getDoctrine()
			->getRepository('VavilenFirstBundle:Post')
			->find($postId);

		$form = $this->createForm(new PostForm(), $post);
		if ($request->getMethod() == 'POST') {
			$form->bind($request);
			if ($form->isValid()) {
				$em = $this->getDoctrine()->getManager();
				$em->persist($form->getData());
				$em->flush();
				return $this->redirect($this->generateUrl('admin'));
			}
		}

		return $this->render('VavilenFirstBundle:Admin:editpost.html.twig', array(
		    'form' => $form->createView(),
		    'post' => $post,
		));
	}

	public function removePostAction($postId)
	{
		$post = $this->getDoctrine()
			->getRepository('VavilenFirstBundle:Post')
			->find($postId);

		$em = $this->getDoctrine()->getManager();
		$em->remove($post);
		$em->flush();

		return $this->redirect($this->generateUrl('admin'));
	}

	public function createCategoryAction(Request $request)
	{
		$form = $this->createForm(new CategoryForm(), new Category());
		if ($request->getMethod() == 'POST') {
			$form->bind($request);
			if ($form->isValid()) {
				$formData = $form->getData();
				$em = $this->getDoctrine()->getManager();
				$em->persist($formData);
				$em->flush();
				return $this->redirect($this->generateUrl('admin'));
			}
		}

		return $this->render('VavilenFirstBundle:Admin:createcategory.html.twig', array(
		    'form' => $form->createView(),
		));
	}

	public function editCategoryAction(Request $request, $categoryId)
	{
		$category = $this->getDoctrine()
			->getRepository('VavilenFirstBundle:Category')
			->find($categoryId);

		$form = $this->createForm(new CategoryForm(), $category);
		if ($request->getMethod() == 'POST') {
			$form->bind($request);
			if ($form->isValid()) {
				$em = $this->getDoctrine()->getManager();
				$em->persist($form->getData());
				$em->flush();
				return $this->redirect($this->generateUrl('admin'));
			}
		}

		return $this->render('VavilenFirstBundle:Admin:editcategory.html.twig', array(
		    'form' => $form->createView(),
		    'category' => $category,
		));
	}
}
